<template>
  <div class="goods-list">
    <div class="goods-item" v-for="item in goodslist" :key="item.id" @click="goodsinfo(item.id, com_name)">
      <img :src="item.img_url" alt>
      <h2 class="title">{{ item.title }}</h2>
      <div class="info">
        <p class="price">
          <span class="now">￥{{ item.now }}</span>
          <span class="old">￥{{ item.old }}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩余{{ item.stock_quantity }}件</span>
        </p>
      </div>
    </div>

    <mt-button type="danger" size="large" @click="getMoreGoods">加载更多</mt-button>
  </div>
</template>

<script>
import { Toast } from "mint-ui";

export default {
  data() {
    return {
      pageindex: 1,
      goodslist: [],
      com_name: 'goodsinfo'
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    getGoodsList() {
      this.$http
        .get("goodslist.php?pageindex=" + this.pageindex)
        .then(result => {
          if (result.body.status === 0) {
            this.goodslist = this.goodslist.concat(result.body.message);
          }
        });
    },
    getMoreGoods() {
      if (++this.pageindex > 2) {
        return Toast("已经没有更多了");
      }
      this.getGoodsList();
    },
    goodsinfo(id, com_name) {
      // console.log(this)
      // 编程式导航
      // 注意： 要区分 this.$route 和 this.$router 的区别
      // 1.
      // this.$router.push('/home/goodsinfo/' + id)
      // 2. 传递对象
      // this.$router.push({path: '/home/goodsinfo/' + id})
      // 3. 
      this.$router.push({ name: 'goodsinfo', params: { id, com_name } })
      // 4.
      // this.$router.push({ name: 'goodsinfo', query: { id: id } })
    }
  }
};
</script>

<style lang="scss" scoped>
.goods-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 7px;

  .goods-item {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    width: 49%;
    margin: 3px 0;
    padding: 2px;
    min-height: 312px;
    border: 1px solid #ccc;
    box-shadow: 0 0 8px #ccc;

    img {
      width: 100%;
    }

    .title {
      font-size: 14px;
    }

    .info {
      padding: 10px;
      background-color: #eee;

      p {
        margin: 0;
      }

      .now {
        color: red;
        font-size: 16px;
        font-weight: bold;
      }

      .old {
        margin-left: 10px;
        text-decoration: line-through;
        font-size: 12px;
      }

      .sell {
        display: flex;
        justify-content: space-between;
        font-size: 12px;
      }
    }
  }
}
</style>
